
package cup.modules.visual.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cup.core.boot.ctrl.CupController;
import cup.core.mp.support.Condition;
import cup.core.mp.support.Query;
import cup.core.oss.MinioTemplate;
import cup.core.oss.model.CupFile;
import cup.core.tool.api.R;
import cup.core.tool.support.Kv;
import cup.core.tool.utils.Func;
import cup.core.tool.utils.ObjectUtil;
import cup.modules.visual.constant.LauncherConstant;
import cup.modules.visual.dto.VisualDTO;
import cup.modules.visual.entity.Visual;
import cup.modules.visual.entity.VisualCategory;
import cup.modules.visual.log.DataLog;
import cup.modules.visual.service.IVisualCategoryService;
import cup.modules.visual.service.IVisualService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.val;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 可视化表 控制器
 *
 *
 */
@RestController
@AllArgsConstructor
@RequestMapping(LauncherConstant.APPLICATION_VISUAL_NAME+"/visual")
@Api(value = "可视化表", tags = "可视化数据接口")
public class VisualController extends CupController {

	private final IVisualService visualService;
	private final IVisualCategoryService categoryService;
//	@Resource
//	private MinioTemplate minioTemplate;



	/**
	 * 详情
	 */
	@DataLog("大屏详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入visual")
	public R<VisualDTO> detail(@RequestParam Long id) {
		VisualDTO detail = visualService.detail(id);
		return R.data(detail);
	}

	/**
	 * 分页 可视化表
	 */
	@DataLog("大屏列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入visual")
	public R<IPage<Visual>> list(Visual visual, Query query) {
		val lambdaQueryWrapper = new LambdaQueryWrapper<Visual>();
		if(ObjectUtil.isNotEmpty(visual.getTitle())) {
			lambdaQueryWrapper.like(Visual::getTitle, visual.getTitle() );
		}
		if(ObjectUtil.isNotEmpty(visual.getCategory())){
			lambdaQueryWrapper.eq(Visual::getCategory,visual.getCategory());
		}
		IPage<Visual> pages = visualService.page(Condition.getPage(query), lambdaQueryWrapper);
		return R.data(pages);
	}

	/**
	 * 新增 可视化表
	 */
	@DataLog("大屏新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增", notes = "传入visual")
	public R save(@Valid @RequestBody VisualDTO visual) {
		boolean temp = visualService.saveVisual(visual);
		if (temp) {
			Long id = visual.getVisual().getId();
			return R.data(Kv.create().set("id", String.valueOf(id)));
		} else {
			return R.status(false);
		}
	}

	/**
	 * 修改 可视化表
	 */
	@DataLog("大屏修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改", notes = "传入visual")
	public R update(@Valid @RequestBody VisualDTO visual) {
		return R.status(visualService.updateVisual(visual));
	}


	/**
	 * 删除 可视化表
	 */
	@DataLog("大屏删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(visualService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 复制 可视化表
	 */
	@DataLog("大屏复制")
	@PostMapping("/copy")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "复制", notes = "传入id")
	public R<Long> copy(@ApiParam(value = "主键集合", required = true) @RequestParam Long id) {
		return R.data(visualService.copyVisual(id));
	}

	/**
	 * 获取分类
	 */
	@GetMapping("category")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "获取类型")
	public R category() {
		List<VisualCategory> list = categoryService.list();
		return R.data(list);
	}

	/**
	 * 上传文件
	 */
	@DataLog("资源上传")
	@SneakyThrows
	@PostMapping("/put-file")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "上传", notes = "传入文件")
	public R<CupFile> putFile(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file) {
//		CupFile cupFile =minioTemplate.putFile(file);
		return R.data(null);
	}

}
